﻿#include "MarketDataSource.h"
#include <time.h> 
#include<string.h>

#define DateTimeChar_Len 8
int MDSrc::GetDateSegment(char pData[9], MData::DateSegment pSegment)
{
	char tempData[9];
	memcpy(tempData, pData, DateTimeChar_Len);
	tempData[DateTimeChar_Len] = 0;
	int dSegment = 0;
	switch (pSegment)
	{
	case MData::Date_Year:
		tempData[4] = 0;
		dSegment = atoi(tempData);
		break;
	case MData::Date_Month:
		tempData[6] = 0;
		dSegment = atoi(&tempData[4]);
		break;
	case MData::Date_Date:
		dSegment = atoi(&tempData[6]);
		break;
	default:
		break;
	}
	return dSegment;
}

int MDSrc::GetTimeSegment(char pTime[9], MData::TimeSegment pSegment)
{
	int dSegment = 0;
	switch (pSegment)
	{
	case MData::Time_Hour:
		dSegment = atoi(&pTime[0]);
		break;
	case MData::Time_Minute:
		dSegment = atoi(&pTime[DateTimeChar_Len - 5]);
		break;
	case MData::Time_Seconds:
		dSegment = atoi(&pTime[DateTimeChar_Len - 2]);
		break;
	default:
		break;
	}
	return dSegment;
}

void MDSrc::SetTimeSegment(char pTime[9], MData::TimeSegment pSegment, int pVal)
{
	switch (pSegment)
	{
	case MData::Time_Hour:
		sprintf(&pTime[0], "%d", pVal);
		break;
	case MData::Time_Minute:
		sprintf(&pTime[DateTimeChar_Len - 5], "%d", pVal);
		break;
	case MData::Time_Seconds:
		sprintf(&pTime[DateTimeChar_Len - 2], "%d", pVal);
		break;
	default:
		break;
	}
}

time_t MDSrc::GetDateTimeStamp(tm pData, char pTime[9])
{
	//tm tm_;
	//tm_.tm_year = GetDateSegment(pData, Date_Year) - 1900;
	//tm_.tm_mon = GetDateSegment(pData, Date_Month) - 1;
	//tm_.tm_mday = GetDateSegment(pData, Date_Date);
	pData.tm_hour = GetTimeSegment(pTime, MData::Time_Hour);
	pData.tm_min = GetTimeSegment(pTime, MData::Time_Minute);
	pData.tm_sec = GetTimeSegment(pTime, MData::Time_Seconds);
	pData.tm_isdst = 0;
	return mktime(&pData); //已经减了8个时区
}

time_t MDSrc::GetDateTimeStampWithoutSecond(tm pData, char pTime[9])
{
	//tm tm_;
	//tm_.tm_year = GetDateSegment(pData, Date_Year) - 1900;
	//tm_.tm_mon = GetDateSegment(pData, Date_Month) - 1;
	//tm_.tm_mday = GetDateSegment(pData, Date_Date);
	pData.tm_hour = GetTimeSegment(pTime, MData::Time_Hour);
	pData.tm_min = GetTimeSegment(pTime, MData::Time_Minute);
	pData.tm_sec = 0;
	pData.tm_isdst = 0;
	return mktime(&pData); //已经减了8个时区
}

time_t MDSrc::GetDateTime(tm today, char pTime[9])
{
	today.tm_hour = GetTimeSegment(pTime, MData::Time_Hour);
	today.tm_min = GetTimeSegment(pTime, MData::Time_Minute);
	today.tm_sec = GetTimeSegment(pTime, MData::Time_Seconds);
	today.tm_isdst = 0;
	return mktime(&today); //已经减了8个时区
}

tm BASEOPTION_EXPORT MDSrc::GetMarketDate(char pData[9])
{
	tm tm_;
	tm_.tm_year = GetDateSegment(pData, MData::Date_Year) - 1900;
	tm_.tm_mon = GetDateSegment(pData, MData::Date_Month) - 1;
	tm_.tm_mday = GetDateSegment(pData, MData::Date_Date);
	tm_.tm_hour = 0;
	tm_.tm_min = 0;
	tm_.tm_sec = 0;
	tm_.tm_isdst = 0;

	return tm_;
}
